package com.hwq.mynavigation;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class MainFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    public MainFragment() {
    }

    public static MainFragment newInstance(String param1, String param2) {
        MainFragment fragment = new MainFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);

        view.findViewById(R.id.tvTwo).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //未采用safe args的传递方式
                Bundle bundle = new Bundle();
                bundle.putString("user_name", "Michael");
                bundle.putInt("age", 30);

                Navigation.findNavController(view).navigate(R.id.action_mainFragment_to_twoFragment,bundle);

            }
        });

        //方法二
//        view.findViewById(R.id.tvGo).setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_mainFragment_to_twoFragment));

        view.findViewById(R.id.tvOne).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //通过safe args完成参数传递
                Bundle bundle = new MainFragmentArgs.Builder()
                        .setUserName("Michael")
                        .setAge(30)
                        .build().toBundle();
                Navigation.findNavController(view).navigate(R.id.action_mainFragment_to_oneFragment, bundle);
            }
        });
        return view;
    }
}